package com.oa.service;


import com.oa.dao.RbacDao;
import com.oa.dao.UserDao;
import com.oa.exception.BussinessException;
import com.oa.pojo.Node;
import com.oa.pojo.User;
import com.oa.utils.MD5Utils;

import java.util.List;

/**
 * ctrl + alt + t 自动生成测试
 */
public class UserService {
    private UserDao userDao = new UserDao();
    private RbacDao rbacDao = new RbacDao();
    /**
     * 检查登录
     * @param username
     * @param password
     * @return L001 用户不存在 L002 密码错误
     */
    public User checkLogin(String username, String password) {
        User user = userDao.selectByUsername(username);

        if (user == null) {
            // 抛出用户不存在异常
            throw new BussinessException("L001", "用户名不存在");
        }

//        if (!password.equals(user.getPassword())) {
//            throw new BussinessException("L002", "密码错误");
//        }

        String md5 = MD5Utils.md5Digest(password, user.getSalt());

        if (!md5.equals(user.getPassword())) {
            throw new BussinessException("L002", "密码错误");
        }

        //检查成功
        return user;
    }

    public List<Node> selectNodeByUserId(Long userId) {
        List<Node> nodeList = rbacDao.selectNodeByUserId(userId);
        return nodeList;
    }


    public void insert(User user){
        userDao.insert(user);
    }
}
